#!/bin/bash

user="phoenix"
group="www-data"

dbuser="user"
dbpass="pass"


vhostroot="/var/www/vhosts"
homeroot="/home/$user/Documents/Projects/web"

company=$(zenity --entry --text "Enter Company:" --entry-text "all")
if [[ $? == 0 ]] ; then
	zenity --warning --text "Cancelled! Exiting..."
	exit
fi

project=$(zenity --entry --text "Enter Project Name:")
if [[ $? == 0 ]] ; then
	zenity --warning --text "Cancelled! Exiting..."
	exit
fi

companyvhost="$vhostroot/$company"
dir="$companyvhost/$project"
homedir="$homeroot/$company/$project"

if [ -d $companyvhost ]
then
	notify-send -t 300 "Company already exists. No need for creation."
else
	notify-send -t 300 "Creating VHosts"   "Creating folders for company"
	mkdir $companyvhost
	mkdir $homeroot/$company
fi

if [ -d $dir ] || [ -d $homedir ]
then
	notify-send -t 300 "Creating VHosts"   "Error: folder already exists!"

else
	notify-send -t 300 "Creating VHosts"   "Creating homedir"
	mkdir $homedir
	notify-send -t 300 "Creating VHosts"   "Linking to web dir"
	ln -s $homedir $dir
	notify-send -t 300 "Creating VHosts"   "Changing ownerships"
	chown $user:$group -R $homeroot/$company $companyvhost
	notify-send -t 300 "Creating VHosts"   "Inserting 127.0.0.1 $project.$company into /etc/hosts"
	echo "127.0.0.1		$project.$company	www.$project.$company" >> /etc/hosts
	notify-send -t 300 "Creating VHosts"   "Done!"
	
	zenity --question --text "Would You create a mysql database for this subdomain?"
	if [[ $? == 0 ]] ; then
		newpass=$(apg -n 1 -m 10 -x 10)
		notify-send -t 300 "Creating VHosts" "Creating MySQL database $company_$project with user $company_$project"
		cmd="create database ${company}_${project};CREATE USER '${company}_${project}'@'localhost' IDENTIFIED BY '$newpass'; GRANT ALL PRIVILEGES ON ${company}_${project}.* TO ${company}_${project}@localhost;"
		echo $(mysql -vvv -u$dbuser -p$dbpass -e "$cmd")
		echo $cmd
		if [[ $? -ne 1 ]] ; then
			zenity --warning --text "Error occured! Could not create database and user! Error: $?"
		else
			zenity --info --text "Created MySQL database and user. 
Database name, username and password:
******************************************************
$company\_$project
$company\_$project
$newpass
******************************************************

Please save that information now because it won't be
shown again."
		fi

		zenity --question --text "Do you want to checkout a svn copy?"
		if [[ $? == 0 ]] ; then
			svnlink=$(zenity --entry --text "SVN Link (including full path to repos)")
			svnuser=$(zenity --entry --text "SVN Username:")
			svnpass=$(zenity --entry --hide-text --text "SVN Password:")
			if [ $svnuser ] && [ $svnlink ] && [ $svnpass ] 
			then
				svn co $svnlink $homedir --username=$svnuser --password=$svnpass | zenity --progress --pulsate --title="SVN Checkout" --text="Please wait for the checkout to complete" --auto-kill --auto-close
				if [[ $? == 0 ]] ; then
					zenity --info --text "Successfully checked out SVN copy"
					notify-send -t 300 "Creating VHosts"   "Creating permissions for SVN"
					chown $user:$group -R $homeroot/$company $companyvhost
				else 
					zenity --warning --text "SVN Checkout failed!"
				fi
			else
				zenity --warning --text "You didn't pass all data for SVN"
			fi
		fi
	fi
fi
